/**
 * Copyright (C) 2011  E-Robot
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 * @brief MapHIMM.cpp
 *
 * @date 9 déc. 2010
 * @author elca
 * @version 0.1
 */

#include "MapHIMM.h"
#include "CellHIMM.h"
#include <QDebug>
#include <QKeyEvent>
#include "../../def.h"
#include "math.h"

MapHIMM::MapHIMM(int _nbrCellWidth, int _nbrCellHeight, int _size,int _resolution ):Map(_nbrCellWidth,_nbrCellHeight,_size,_resolution)
{
	useGRO = false;

	coefHIMM[0][0] = 0.5;coefHIMM[0][1] = 0.5;coefHIMM[0][2] = 0.5;
	coefHIMM[1][0] = 0.5;coefHIMM[1][1] =   1;coefHIMM[1][2] = 0.5;
	coefHIMM[2][0] = 0.5;coefHIMM[2][1] = 0.5;coefHIMM[2][2] = 0.5;
	//Création des cellules
	int xI,yI;
	for(int i=0;i<nbCellHeight;i++){
		cells.append(QVector<Cell*>());
		for(int j=0;j<nbCellWidth;j++){
			xI= xScene +j*size;
			yI= yScene +i*size;

			cells[i].append(new CellHIMM(xI,yI,size,size,this));
			connect(this,SIGNAL(changeMask(int)),cells[i].at(j),SLOT(changeMask(int)));
			connect(this,SIGNAL(resetMap()),cells[i].at(j),SLOT(resetCell()));

		}
	}


}

void MapHIMM::keyPressEvent ( QKeyEvent * event ){
	Map::keyPressEvent(event);
	if(event->key()==Qt::Key_G){
		useGRO = !useGRO;
		qDebug()<< "Is GRO used: "<<useGRO;
	}

}

void MapHIMM::updateCell(QLineF _line){
		static double l;
		static int tmpVal;
		static int max;
		static QGraphicsLineItem litem;
		static int size_res = size*resolution;

		l = _line.length();

		//Position dans tableau de x et y début,fin
		static int  yS, yE,xS,xE;
		yS = getY(_line.p1().y());
		yE = getY(_line.p2().y());
		xS = getX(_line.p1().x());
		xE = getX(_line.p2().x());

		//Item de test pour collision
		//items.at(nbsonar)->setLine(_line.p1().x()*size/resolution,-1*_line.p1().y()*size/resolution,_line.p2().x()*size/resolution,-1*_line.p2().y()*size/resolution);
		litem.setLine(_line.p1().x()*size_res,-1*_line.p1().y()*size_res,_line.p2().x()*size_res,-1*_line.p2().y()*size_res);
	//	qDebug()<<_line;
	//	qDebug()<< _line.p1().x()*size_res<<" "<<-1*_line.p1().y()*size_res<<" "<<_line.p2().x()*size_res<<" "<<-1*_line.p2().y()*size_res;
	//	qDebug()<< "yS:"<<yS<< "yE:"<<yE<< "xS:"<<xS<< "xE:"<<xE;
		//Parcours rectangle formé par la ligne
		for(int j = yS;(yS<=yE)?j<=yE:j>=yE;(yS<=yE)?j++:j--){
			for(int i = xS;(xS<=xE)?i<=xE:i>=xE;(xS<=xE)?i++:i--){

				//S'il y a collision faudra incrémenter la cellule
				if(litem.collidesWithItem(cells[j].at(i))){
	//				qDebug()<<j<< "____ "<<i;
					mut.lock();
					//Incrément de 3 ssi distance < max et on est en bout de ligne
					if(!(fabs(l-MAX_DISTANCE_SONAR)<0.0001) && i==xE && j == yE){
						((CellHIMM*)cells[j].at(i))->increment(3);
						//Use Growth rate operator
						if(useGRO){
							tmpVal=0;
							max = 0;
							for(int k=0;k<3;k++){
								for(int l=0;l<3;l++){
									if((j+k-1)> 0 && (i+l-1)>0 && (j+k-1)< cells.size() && (i+l-1)<cells[0].size()){

										if(((CellHIMM*)cells[j+k-1].at(i+l-1))->value()>max){
											max = ((CellHIMM*)cells[j+k-1].at(i+l-1))->value();
										}
										tmpVal += ((CellHIMM*)cells[j+k-1].at(i+l-1))->value()*coefHIMM[k][l];
									}
								}
							}

							if(tmpVal< max){
								cells[j].at(i)->setValue(tmpVal);
							}else{
								cells[j].at(i)->setValue(max);
							}
						}
					}else{
						((CellHIMM*)cells[j].at(i))->increment(-1);
					}
					mut.unlock();

				}

			}
		}
		update(xScene,yScene,widthScene,heightScene);
}
MapHIMM::~MapHIMM()
{
	// TODO Auto-generated destructor stub
}
